import { updateResFile, uploadFiles} from '../util/file'
const dataLocation = `${__dirname}/../data/catalogues.json`

// 上传文件
export const uploadFile = files => {
  return uploadFiles(files)
}

// 添加文件
export const addFile = async ({files, parentNode, mainId, tree}) => {
  const htmlNode = files.find(item => item.path.endsWith(`.html`))
  
  const newChildren = files.map(item => {
    const data = {
      label: item.fileName,
      parent: mainId,
      type: 2,
      mainId: ++tree.maxId
    }
    if (item.fileName.endsWith(`.html`)) {
      data.src = htmlNode.path
    }
    data.fileDir = htmlNode.fileDir
    return data
  })
  if (newChildren.length === 2) {
    newChildren[0].bro = newChildren[1].mainId
    newChildren[1].bro = newChildren[0].mainId
  }
  // 将文件节点放到父节点中
  parentNode.children = [
    ...parentNode.children,
    ...newChildren
  ]
  
  return await updateResFile(dataLocation, tree)
}

export default {
  uploadFile,
  addFile
}
